package com.hctforgreen.greenservice;

import java.util.Locale;

import android.content.Intent;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CheckBox;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hctforgreen.greenservice.utils.LanguageSettingUtil;
import com.hctforgreen.greenservice.utils.ThemeStoreUtil;

/**
 * 多语言设置
 * 
 * @author denghb
 * 
 */
public class LanguageSettingActivity extends ParentActivity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_download_setting);
		initSkinLayout();
		initTitleButtonBar();
		initWindow();
	}


	private void initTitleButtonBar() {
		final TextView titleTv = (TextView) findViewById(R.id.tv_title);
		titleTv.setText(getString(R.string.download_stroe_setting_name));

		final ImageButton backBtn = (ImageButton) findViewById(R.id.btn_back);
		backBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				finish();
			}
		});
	}

	private void initWindow() {

		final CheckBox cbInner = (CheckBox) findViewById(R.id.cb_inner);
		final TextView tvChinese = (TextView) findViewById(R.id.tv_first_setting);
		final TextView tvEnglish = (TextView) findViewById(R.id.tv_second_setting);

		tvChinese.setText(getResources().getString(
				R.string.language_setting_chinese));
		tvEnglish.setText(getResources().getString(
				R.string.language_setting_english));
		final CheckBox cbOutter = (CheckBox) findViewById(R.id.cb_outter);
		// final View pbInner = findViewById(R.id.inner_loading);
		final View innerSd = findViewById(R.id.lyt_download_inner_sd);
		// final View pbOutter = findViewById(R.id.outter_loading);
		final View outterSd = findViewById(R.id.lyt_download_outter_sd);
		innerSd.setOnClickListener(new OnClickListener() {// 设置为中文版

			@Override
			public void onClick(View v) {
				LanguageSettingUtil.save(getApplicationContext(), 0);
				cbInner.setChecked(true);
				cbOutter.setChecked(false);
				switchLanguage(Locale.CHINESE);
			}
		});

		outterSd.setOnClickListener(new OnClickListener() {// 设置为英文版

			@Override
			public void onClick(View v) {
				LanguageSettingUtil.save(getApplicationContext(), 1);
				cbOutter.setChecked(true);
				cbInner.setChecked(false);
				switchLanguage(Locale.ENGLISH);

			}
		});

		int setting = LanguageSettingUtil.get(getApplicationContext());
		switch (setting) {
		case -1:// 未设置
			Locale locale = getResources().getConfiguration().locale;
			String language = locale.getLanguage();
			if (language.endsWith("zh")) {// 中文版
				cbInner.setChecked(true);
				cbOutter.setChecked(false);
			} else {
				cbInner.setChecked(true);
				cbOutter.setChecked(false);
			}

			break;
		case 0:// 中文版
			cbInner.setChecked(true);
			cbOutter.setChecked(false);
			break;
		case 1:// 英文版
			cbInner.setChecked(false);
			cbOutter.setChecked(true);
			break;
		}

	}

	private void switchLanguage(Locale locale) {
		Resources resources = getResources();// 获得res资源对象
		Configuration config = resources.getConfiguration();// 获得设置对象
		DisplayMetrics dm = resources.getDisplayMetrics();// 获得屏幕参数：主要是分辨率，像素等。
		config.locale = locale; // 简体中文
		resources.updateConfiguration(config, dm);
		Intent broad = new Intent(MainActivityV2.SWITCH_LANGUAGE_ACTION_NAME);
		sendBroadcast(broad);

		Intent it = new Intent(LanguageSettingActivity.this,
				WelcomeActivity.class);
		it.putExtra("second_time", "true");
		it.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		it.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		startActivity(it);
		finish();

	}
}
